/**
 * Created by yi on 2017-01-06.
 */
import Vue from "vue";
import Vuex from "vuex";
// import * as actions from './actions';
import * as getters from "./getters";
// 告诉 vue “使用” vuex
Vue.use(Vuex);
let appLoading = document.getElementById("appLoading");

// 创建一个对象来保存应用启动时的初始状态
const state = {
  storeInfo: {}, //店铺信息
  source: 0, // 积分
  role: "2", //角色
  balance: 0, //余额
  storeid: "1", //商铺id
  loadingShow: true, //是否显示loading
  path: "https://www.jubenxiong.com/games/dianwu/php/index.php/", //http://192.168.2.232/dianwu/php/index.php/   https://www.jubenxiong.com/games/dianwu/php/index.php/
  soursePath: "https://www.jubenxiong.com/games/dianwu/admin",
  soursePathTwo: "https://www.jubenxiong.com/games/dianwu/admin",
  phoneno: "", // 用户手机号码等与""时候表示未绑定手机
  serviceTel: "13916403296", //客服电话
  icon: "", //头像路劲（绝对路径）
  openid: "", //微信openid
  userid: "1", //用户id
  username: "午夜屠蚊男", //微信昵称
  realname: "", //用户真实姓名
  sex: "女", //性别
  birthday: "1990-01-01",
  seturl: "",
  wxappid: "wx7d05a96d073b899b",
  domainUrl: "https://www.jubenxiong.com/games/dianwu/", //微信功能目录
  shareUrl: "https://www.jubenxiong.com/",     //分享扫码相关
  sistem: "", //系统为ios|Android
  consumeOkPopupShow: false, //是否显示消费成功弹出框
  gameGiftPopupShow: false, //是否显示礼品领取弹出框
  giftList:null,            //当前是否有奖励需要领取
  iosPath:null      //ios微信验证的url
};
// 创建一个对象存储一系列我们接下来要写的 mutation 函数
const mutations = {
  UPDATA_SHOW_GIFT_POPUP(state, value){
    state.gameGiftPopupShow = value;
  },
  UPDATA_GIFT_LIST(state, value){
    state.giftList = value;
  },
  //更新signUrl
  UPDATA_IOS_PATH(state, value){
    state.iosPath = value;
  },
  // TODO: 放置我们的状态变更函数
  UPDATE_STORE_INFO(state, value) {
    state.storeInfo = value;
  },
  UPDATE_SOURCE(state, value) {
    state.source = value;
  },
  //更新商铺id
  UPDATE_STOREID(state, value) {
    state.storeid = value;
  },
  UPDATE_SEX(state, value) {
    state.sex = value;
  },
  UPDATE_BIRTHDAY(state, value) {
    state.birthday = value;
  },
  UPDATE_REALNAME(state, value) {
    state.realname = value;
  },
  UPDATE_BALANCE(state, value) {
    state.balance = value;
  },
  UPDATE_ROLE(state, value) {
    state.role = value;
  },
  UPDATE_SERVICE_TEL(state, value) {
    state.serviceTel = value;
  },
  UPDATE_LOADING(state, value) {
    //是否显示loading
    state.loadingShow = value;
    if (value) {
      if (!appLoading.classList.contains("hide")) return;
      appLoading.classList.remove("hide");
    } else {
      if (appLoading.classList.contains("hide")) return;
      appLoading.classList.add("hide");
    }
  },
  UPDATE_PHONENO(state, value) {
    //更新用户手机号码
    state.phoneno = value;
  },
  UPDATE_ICON(state, value) {
    //更新头像路劲
    state.icon = value;
  },
  UPDATE_OPNEID(state, value) {
    //更新openid
    state.openid = value;
  },
  UPDATE_USERID(state, value) {
    //更新userid
    state.userid = value;
  },
  UPDATE_USERNAME(state, value) {
    //更新username
    state.username = value;
  },
  UPDATE_CONSUMEOKSHOW(state, value) {
    //更新显示消费成功弹出框
    //是否显示消费成功弹出框
    state.consumeOkPopupShow = value;
  },
  SETURL(state, value) {
    //分享链接
    state.seturl = value;
  },
  UPDATA_SYSTEM(state, value) {
    //系统
    state.sistem = value;
  }
};

let loadingClearId = -1000; //延迟显示loading clearID
let loadingMark = false; //标记当前loading是否需要显示
let mustId = -1000; //loading必须显示一定时间的clearID

const actions = {
  toggleLoading(context, value) {
    //loading延迟200ms显示,避免频繁显现
    loadingMark = value;
    if (value) {
      if (state.loadingShow) return;
      if (loadingClearId != -1000) return;
      loadingClearId = window.setTimeout(() => {
        window.clearTimeout(loadingClearId);
        loadingClearId = -1000;
        context.commit("UPDATE_LOADING", true);
        context.dispatch("mustShowLoading");
      }, 0);
    } else {
      if (state.loadingShow) {
        if (mustId != -1000) return;
        context.commit("UPDATE_LOADING", false);
      } else {
        if (loadingClearId != -1000) {
          window.clearTimeout(loadingClearId);
        }
      }
    }
  },
  mustShowLoading(context) {
    //loading必须显示500ms
    mustId = window.setTimeout(() => {
      window.clearTimeout(mustId);
      mustId = -1000;
      if (!loadingMark) context.commit("UPDATE_LOADING", false);
    }, 500);
  }
};

// 整合初始状态和变更函数，我们就得到了我们所需的 store
// 至此，这个 store 就可以连接到我们的应用中
export default new Vuex.Store({
  state,
  mutations,
  actions,
  getters
});
